﻿using System.Collections.Generic;
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
using Yunchang;
using System;
using AutoChess;

public class RoundStatePanel : MonoBehaviour
{
    public Text round;
    public Text time;
    public Text stateText;
    public GameObject waitFightEnd, shalou;

    public Text leftPlayerName,RightPlayerName;
    public GameObject fightNameRoot, fightTimeRoot,VSRoot;
    private void OnEnable()
    {
        EventManager.AddEventListener(CommonEvent.ON_AUTOCHESS_BEGIN_FIGHT_COUNT_DOWN, BeginFightCount);//回合开始计时
        EventManager.AddEventListener(CommonEvent.ON_AUTOCHESS_END_FIGHT_COUNT_DOWN, EndFightCount);    //切换战斗停止计时
        EventManager.AddEventListener(CommonEvent.ON_CUR_FIGHT_END, OnCurFightEnd);                      //自己战斗结束计时
    }
    private void OnDisable()
    {
        EventManager.RemoveEventListener(CommonEvent.ON_AUTOCHESS_BEGIN_FIGHT_COUNT_DOWN, BeginFightCount);
        EventManager.RemoveEventListener(CommonEvent.ON_AUTOCHESS_END_FIGHT_COUNT_DOWN, EndFightCount);
        EventManager.RemoveEventListener(CommonEvent.ON_CUR_FIGHT_END, OnCurFightEnd);
    }
    private void EndFightCount()  //切换战斗的时候 停止计时
    {
        StopAllCoroutines();
    }
    private void OnCurFightEnd()  //战斗结束的时候
    {
        StopAllCoroutines();
        waitFightEnd.SetActive(true);
        shalou.SetActiveEx(false);
    }

    private void BeginFightCount() //开始战斗倒计时
    {
        StopAllCoroutines();
        StartCoroutine(StartFightTimeEnd());
    }
    /// <summary>
    /// 服务器状态更新可切换玩家
    /// </summary>
    public void RefreshState()
    {
        var cacheData = AutoChess.AutoChessManager.Ins.cacheData;
        round.text = cacheData.RoundNum.ToString();
        waitFightEnd.SetActive(false);
        shalou.SetActiveEx(false);
        long targetTime = 0;
        if (cacheData.ServerDataState == ProtoBuf.Message.AutoChessStateType.AUTO_CHESS_STATE_ADJUST)
        {
            targetTime = cacheData.AdjustEndTime;
            stateText.text = "调整阶段";
            StopAllCoroutines();
            StartCoroutine("StartTimeEnd", targetTime);

            VSRoot.SetActive(false);
            fightNameRoot.SetActive(false);
            fightTimeRoot.SetActive(true);
        }
        else if (cacheData.ServerDataState == ProtoBuf.Message.AutoChessStateType.AUTO_CHESS_STATE_FIGHTING)
        {
            stateText.text = "战斗阶段";
            targetTime = cacheData.FightEndTime;
            StopAllCoroutines();
            StartCoroutine(StartFightTimeEnd());
            var curentValue = (int)AutoChess.AutoChessManager.Ins.CurrentAutoChessField.fightCtr.CurFightPassTime;
            if (curentValue <= 0)
            {
                curentValue = 0;
                waitFightEnd.SetActive(true);
                shalou.SetActiveEx(false);
            }
            time.text = curentValue.ToString();

            VSRoot.SetActive(true);
            fightNameRoot.SetActive(true);
            fightTimeRoot.SetActive(false);
        }
        if (AutoChess.AutoChessManager.Ins.cacheData.CurPlayerData.oneMatchRes != null)
        {
            RightPlayerName.text = AutoChess.AutoChessManager.Ins.cacheData.CurPlayerData.oneMatchRes.right.name;
            leftPlayerName.text = AutoChess.AutoChessManager.Ins.cacheData.CurPlayerData.oneMatchRes.left.name;
        }
        else
        {
            VSRoot.SetActive(false);
            fightNameRoot.SetActive(false);
            fightTimeRoot.SetActive(true);
        }
    }
    //战斗阶段倒计时
    private IEnumerator StartFightTimeEnd()
    {
        waitFightEnd.SetActive(false);
        shalou.SetActiveEx(false);
        while (AutoChess.AutoChessManager.Ins.CurrentAutoChessField.fightCtr.CurFightPassTime > 0)
        {
            var intValue = ((int)AutoChess.AutoChessManager.Ins.CurrentAutoChessField.fightCtr.CurFightPassTime);
            if (intValue <= 5)
            {
                time.text = string.Format("<color=red>{0}</color>", intValue);
                CommonEffectManager.Instance.PlayEffectImmediate(CommonEffectResId.UI_150_zizhouqi_jishi, time.transform);
            }
            else
            {
                time.text = intValue.ToString();
            }
            yield return new WaitForSecondsRealtime(1);
        }
        waitFightEnd.SetActive(true);
        shalou.SetActiveEx(false);
    }
    //服务器时间倒计时
    private IEnumerator StartTimeEnd(long target)
    {
        var left = Mathf.CeilToInt((target - ServerTimerTool.CurrentTime.Ticks)/ 10000000f);
        //Debug.LogError(string.Format("Target:{0}，curTick:{1},left:{2}", target, ServerTimerTool.CurrentTime.Ticks,left));
        while (left >= 0)
        {
            if (left <= 5)
            {
                time.text = string.Format("<color=red>{0}</color>", left);
                CommonEffectManager.Instance.PlayEffectImmediate(CommonEffectResId.UI_150_zizhouqi_jishi, time.transform);
            }
            else
            {
                time.text = left.ToString();
            }
            yield return new WaitForSecondsRealtime(1);
            left  = Mathf.CeilToInt((target - ServerTimerTool.CurrentTime.Ticks) / 10000000f);
        }
        time.text = string.Format("<color=red>{0}</color>", 0);
    }
}
